package logic

import (
	"context"
	"monitor/user/api/internal/svc"
	"monitor/user/api/internal/types"
	"monitor/user/model"
	"time"

	"github.com/tal-tech/go-zero/core/logx"
)

type TermAlarmsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewTermAlarmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) TermAlarmsLogic {
	return TermAlarmsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TermAlarmsLogic) TermAlarms(req types.PageReq, token string) (*types.AlarmReplyS, error) {
	// todo: add your logic here and delete this line
	claims := ParseToken(token)
	//分页查询
	reply := []types.AlarmReply{}
	count, termAlarms, err := l.svcCtx.AlarmModel.FindAllAlarms(req.Page, req.Limit, claims["city"].(string))
	//fmt.Printf("count:'%d,userInfos:%#v,err = %v\n", count, userInfos, err)
	switch err {
	case nil:
		for _, val := range termAlarms {
			//fmt.Printf("Datetime: %#v\n", val.Datetime)
			t, _ := time.Parse("2006-01-02T15:04:05Z07:00", val.Datetime)
			//fmt.Printf("t: %#v\n", t.Format("2006-01-02 15:04:05"))
			//fmt.Printf("t-unix: %v\n", t.Unix())
			rep := types.AlarmReply{
				Id:       val.Id,
				Sn:       val.Sn,
				Posno:    val.Posno,
				City:     val.City,
				TyId:     val.Tyid,
				Sver:     val.Sver,
				Unum1:    uint(val.Unum1),
				Unum2:    uint(val.Unum2),
				Datetime: t.Unix(),
				Amount:   uint(val.Amount),
				Count:    uint(val.Count),
				Line:     uint(val.Line),
				Carno:    val.Carno,
				Jd:       val.Jd,
				Wd:       val.Wd,
				Alarm:    int(val.Alarm),
				Pver:     uint(val.Pver),
				Bver:     uint(val.Bver),
				Empid:    val.Empid,
				Rvs1:     val.Rvs1,
				Rvs2:     val.Rvs2,
				Stime:    val.Stime,
				Ctime:    val.Ctime,
				Info:     val.Info,
			}
			reply = append(reply, rep)
		}
		return &types.AlarmReplyS{Count: count, Reply: reply}, nil
	case model.ErrNotFound:
		return nil, errorRecNotFound
	default:
		return nil, err
	}
}
